<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 数组去重的方法
      let arr = [1, 5, 2, 1, 5, 66, 2];
      //1.es6的set方法
      let newArr = Array.from(new Set(arr));
      // 2.借助数组的indexOf方法
      let arr1 = [];
      arr.forEach((ele, i) => {
        if (arr1.indexOf(arr[i]) === -1) {
          arr1.push(arr[i]);
        }
      });

      //递归查找
      const obj = [
        {
          id: 1,
          name: "1",
          children: [
            {
              id: "11",
              name: "11",
              children: [
                {
                  id: "111",
                  name: "111",
                },
              ],
            },
            {
              id: "12",
              name: "12",
            },
          ],
        },
        {
          id: 2,
          name: "2",
          children: [],
        },
      ];
      const targetId = "12";
      let res = null;
      function getDataById(id, obj) {
        obj.forEach(function (item) {
          if (item.id == id) {
            res = item;
          }
          if (item.children) {
            this.getDataById(id, item.children);
          }
        });
        return res;
      }

      console.log(getDataById(1, obj));
    </script>
  </body>
</html>
